package com.ruoyi.web.controller.applet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.applet.domain.MiniLeague;
import com.ruoyi.applet.domain.MiniPledgeNum;
import com.ruoyi.applet.service.IMiniLeagueService;
import com.ruoyi.applet.service.IMiniPledgeNumService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.applet.domain.MiniQualityGuarantee;
import com.ruoyi.applet.service.IMiniQualityGuaranteeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质保管理Controller
 *
 * @author liucan
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/applet/guarantee")
public class MiniQualityGuaranteeController extends BaseController {
    @Resource
    private IMiniQualityGuaranteeService miniQualityGuaranteeService;

    @Resource
    private IMiniPledgeNumService iMiniPledgeNumService;

    @Resource
    private MiniLeagueController miniLeagueController;

    @Autowired
    private IMiniLeagueService miniLeagueService;
    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询质保管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@AuthenticationPrincipal UserDetails userDetails, MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        //先查询加盟商存不存在如果不存在根据toApplet查询
        String toApplet = userDetails.getUsername();
        List<MiniQualityGuarantee> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        SysUser user =new SysUser();
        user=iSysUserService.selectUserByUserName(toApplet);
        if(toApplet.equals("admin")){
            startPage();
            miniQualityGuarantee.setToApplet(toApplet);
            list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
//            map.put("list",list);
//            map.put("status",0);
            return getDataTable(list,user.getUserRole());
        }
        if(user.getUserRole()==0){
            startPage();
            miniQualityGuarantee.setToApplet(toApplet);
            list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
//            map.put("list",list);
//            map.put("status",0);
            return getDataTable(list,user.getUserRole());
        }
        //根据用户的电话，查询加盟商的信息，获取加盟商的id
        MiniLeague miniLeague = new MiniLeague();
        miniLeague.setLeNumber(toApplet);
        miniLeague.setLeStatus(1L);
        miniLeague = miniLeagueService.selectMiniLeagueList(miniLeague).get(0);
        //如果存在就根据手机号查询出加盟商的id，根据加盟商店铺的id查询出加盟商的信息
        Long id = miniLeague.getId();
        startPage();
        miniQualityGuarantee.setQuShop(id.toString().trim());
        miniQualityGuarantee.setToApplet(miniLeague.getToApplet());
        list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
//        map.put("list",list);
//        map.put("status",1);
        return getDataTable(list,user.getUserRole());
    }


    /**
     * 小程序首页录入质保页面中的质保编码查询
     */
    @GetMapping("/selectNum")
    public AjaxResult selectNum(MiniQualityGuarantee miniQualityGuarantee) {
        //查询客户选择的膜类型和产品品牌和产品型号查询  MiniPledgeNum质保编码对象表启用状态 对应的生成规则、位数和最后一个到几号了
        MiniPledgeNum miniPledgeNum = new MiniPledgeNum();
        miniPledgeNum.setDictId(miniQualityGuarantee.getQuTypeId());//产品类型
        miniPledgeNum.setBrId(miniQualityGuarantee.getQuCaBrand());//产品品牌
        miniPledgeNum.setPrId(miniQualityGuarantee.getPrModel());//产品型号
        MiniPledgeNum miniPledgeNum1 = iMiniPledgeNumService.selectMiniPledgeNumList(miniPledgeNum).get(0);
        String coRule = miniPledgeNum1.getCoRule();//生成规则
        Integer coPlace = Math.toIntExact(miniPledgeNum1.getCoPlace());//位数
        Integer coSome = miniPledgeNum1.getCoSome() + 1;//到几号
        String paddedNumber = String.format(coRule + "%0" + coPlace + "d", coSome);
        miniQualityGuarantee.setQuQualityNum(paddedNumber);//质保编号
        miniQualityGuarantee.setCoSome(coSome);
        miniQualityGuarantee.setPnId(miniPledgeNum1.getId());//质保编号的id
        return AjaxResult.success(miniQualityGuarantee);
    }

    /**
     * 新增质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        String toApplet = request.getHeader("toApplet");
        miniQualityGuarantee.setToApplet(toApplet);
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        return miniQualityGuaranteeService.insertMiniQualityGuarantee(miniQualityGuarantee);
    }

    /**
     * 修改质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        return toAjax(miniQualityGuaranteeService.updateMiniQualityGuarantee(miniQualityGuarantee));
    }

    /**
     * 删除质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniQualityGuaranteeService.deleteMiniQualityGuaranteeByIds(ids));
    }

    /**
     * 根据手机号、质保卡号、车架号、车牌号查询
     */
    @GetMapping("/select")
    public AjaxResult select(MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        List<MiniQualityGuarantee> list = miniQualityGuaranteeService.select(miniQualityGuarantee);
//        list.forEach(miniQualityGuarantee1 -> {
//            System.out.println("miniQualityGuarantee1==="+miniQualityGuarantee1);
//        });
        return AjaxResult.success(list);
    }


    /**
     * 导出质保管理列表
     */
    @Log(title = "质保管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniQualityGuarantee miniQualityGuarantee) {
        List<MiniQualityGuarantee> list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
        ExcelUtil<MiniQualityGuarantee> util = new ExcelUtil<MiniQualityGuarantee>(MiniQualityGuarantee.class);
        util.exportExcel(response, list, "质保管理数据");
    }

    /**
     * 获取质保管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniQualityGuaranteeService.selectMiniQualityGuaranteeById(id));
    }
}
